/*	$Id: player.c 90 2007-01-05 16:47:41Z phrakt $	*/
/*
 * Copyright (c) 2006 Jean-Francois Brousseau <phrakt@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <sys/types.h>

#include <string.h>
#include <stdlib.h>
#include <unistd.h>

#include "mtg.h"
#include "player.h"

/*
 * mtg_player_create()
 *
 */
mtg_player_t*
mtg_player_create(void)
{
	mtg_player_t *player;

	if ((player = malloc(sizeof(*player))) == NULL) {
		return (NULL);
	}

	player->mp_life = MTG_PLAYER_INITLIFE;
	player->mp_poison = 0;

	mtg_manapool_init(&player->mp_pool);

	return (player);
}

/*
 * mtg_player_free()
 *
 */
void
mtg_player_free(mtg_player_t *p)
{
	if (p != NULL) {
		if (p->mp_name != NULL)
			free(p->mp_name);
		free(p);
	}
}

/*
 * mtg_player_init()
 *
 * Reinitialize a player's parameters to start a new game.
 */
int
mtg_player_init(mtg_player_t *p)
{

	p->mp_life = MTG_PLAYER_INITLIFE;
	p->mp_poison = 0;

	mtg_manapool_init(&p->mp_pool);

	return (0);
}

/*
 * mtg_player_getlife()
 */
int
mtg_player_getlife(const mtg_player_t *p)
{
	return (p->mp_life);
}

/*
 * mtg_player_getname()
 *
 */
const char*
mtg_player_getname(const mtg_player_t *p)
{
	return (p->mp_name);
}

/*
 * mtg_player_setlife()
 *
 */
void
mtg_player_setlife(mtg_player_t *p, int life)
{
	p->mp_life = life;
}

/*
 * mtg_player_setname()
 *
 * Returns 0 on success, or -1 on failure.
 */
int
mtg_player_setname(mtg_player_t *p, const char *name)
{
	char *tmp;

	if ((tmp = strdup(name)) == NULL)
		return (-1);

	if (p->mp_name != NULL)
		free(p->mp_name);
	p->mp_name = tmp;
	return (0);
}
